'''
Back end for the foodler workflow (Winter Intensives Lab 5)

This file is where you "wire" together the steps in the process.
'''

# these next few lines import some of the WMP functions we will
# use in this file.
from backend.backend import Backend
from workflow.task import Task
# from workflow.result import Result # uncomment this line if using Result objects (not needed in simple workflow)
from workflow.flowData import Status

class FoodlerBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "EricHoldenFoodler")  # be sure you update the workflow name

    def wire(self):
        self.register_result_listener("Customer", "PlaceOrder", self.order_placed)
        self.register_result_listener("Foodler", "TakeOrder", self.order_taken)
        self.register_result_listener("Restaurant", "ReceiveOrder", self.order_received)
        self.register_result_listener("Restaurant", "CompleteOrder", self.order_completed)
        self.register_result_listener("Restaurant", "RestaurantDeliverComplete", self.restaurant_deliver_completed)
        self.register_result_listener("Deliver", "DeliverPickup", self.deliver_pickedup)
        self.register_result_listener("Deliver", "DeliverComplete", self.deliver_completed)


    def order_placed(self, results):
        for Result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(Result, "Foodler", "TakeOrder") 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(Result, Status.COMPLETE)
            
    def order_taken(self, results):
        for Result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(Result, "Restaurant", "ReceiveOrder") 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(Result, Status.COMPLETE)
            
    def order_received(self, results):
        for Result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(Result, "Restaurant", "CompleteOrder") 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(Result, Status.COMPLETE)

    def order_completed(self, results):
        for Result in results:  # repeat the following actions for each result
            
            delivery = Result.data['delivery']
            
            if delivery == "TRUE":
                task = Task.construct_from_result(Result, "Restaurant", "RestaurantDeliverComplete") 
            else:
                task = Task.construct_from_result(Result, "Deliver", "DeliverPickup") 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(Result, Status.COMPLETE)


    def deliver_pickedup(self, results):
        for Result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(Result, "Deliver", "DeliverComplete") 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(Result, Status.COMPLETE)

    def restaurant_deliver_completed(self, results):
        for Result in results:  # repeat the following actions for each result
            self.workflow.update_status(Result, Status.COMPLETE)

    def deliver_completed(self, results):
        for Result in results:  # repeat the following actions for each result
            self.workflow.update_status(Result, Status.COMPLETE)

if __name__ == '__main__':
    backend = FoodlerBackend()